package a51qindian.com.comprehensive.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.li.widget.DoubleTextView;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import a51qindian.com.comprehensive.Const;
import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.adapter.ShopDeviceAdapter;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.items.LinearItemDecoration;
import a51qindian.com.comprehensive.model.CountBaseModel;
import a51qindian.com.comprehensive.model.ShopDeviceInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import butterknife.BindView;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;


/**
 * Author: Li
 * Date: 2017/10/25 0025
 * Description: 设备列表
 */

public class DeviceListActivity extends QDBaseActivity {


    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler_list)
    RecyclerView mRecyclerList;
    @BindView(R.id.layout_refresh)
    SmartRefreshLayout mLayoutRefresh;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;
    @BindView(R.id.dtv_num)
    DoubleTextView mDtvNum;

    private ShopDeviceAdapter mDeviceAdapter;
    private List<ShopDeviceInfo> mShopDeviceInfoList;
    private String mShopId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_list;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mShopId = getIntent().getStringExtra("shopId");
        setSmartRefreshLayout(mLayoutRefresh);
        mTopNavigation.setNavigationBarListener(this);
        mShopDeviceInfoList = new ArrayList<>();
        mDeviceAdapter = new ShopDeviceAdapter(this);
        mRecyclerList.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerList.addItemDecoration(new LinearItemDecoration());
        mRecyclerList.setAdapter(mDeviceAdapter);
        showShopDevice(mUid, mShopId, Const.DEFAULT_PAGE);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        super.onLoadmore(refreshlayout);
        showShopDevice(mUid,mShopId,String.valueOf(mPage+1));
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        showShopDevice(mUid,mShopId,Const.DEFAULT_PAGE);
    }

    private void showShopDevice(String uid, final String shopId, String page) {
        mLayoutContent.onLoadingWithContent();
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("shop_id", shopId);
        map.put("page", page);
        ObservableHelper.createObservable(RetrofitCreator.createService().shopDevice(map))
                .subscribe(new SelectorObserver<CountBaseModel<List<ShopDeviceInfo>>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        stopRefreshLoad();
                        mLayoutRefresh.setLoadmoreFinished(true);
                        showToast(info);
                        mLayoutContent.onSuccess();
                    }

                    @Override
                    protected void onSuccess(CountBaseModel<List<ShopDeviceInfo>> info) {
                        List<ShopDeviceInfo> deviceInfoList = info.getData();
                        mLayoutContent.onSuccess();
                        stopRefreshLoad();
                        if (mIsRefresh) {
                            mPage = 0;
                            mShopDeviceInfoList.clear();
                            mLayoutRefresh.setLoadmoreFinished(false);
                        } else {
                            mPage++;
                        }
                        if (!mIsRefresh && deviceInfoList.size() < Const.DEFAULT_PAGE_SIZE) {
                            mLayoutRefresh.setLoadmoreFinished(true);
                        }
                        mShopDeviceInfoList.addAll(deviceInfoList);
                        mDeviceAdapter.setShopDeviceInfoList(mShopDeviceInfoList);
                        mDtvNum.setTxtRight(info.getCount());
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        showToast(e.getMessage());
                        stopRefreshLoad();
                    }
                });
    }

}
